package ru.ifmo.ct.network.brafs.server;

import java.util.ArrayList;
import java.util.List;

public class Segment {
    String filename;
    long id;
    List<Server.Client> clients;

    public Segment(String filename, long id) {
        this.filename = filename;
        this.id = id;
        clients = new ArrayList<>();
    }

    public List<Server.Client> getClients() {
        return clients;
    }

    public void addClient(Server.Client client) {
        clients.add(client);
    }

    public Server.Client getOtherClient(Server.Client client) {
        for (Server.Client cl : clients) {
            if (!cl.equals(client)) {
                return cl; //TODO select random
            }
        }
        throw new AssertionError();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Segment)) return false;

        Segment segment = (Segment) o;

        if (id != segment.id) return false;
        if (filename != null ? !filename.equals(segment.filename) : segment.filename != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = filename != null ? filename.hashCode() : 0;
        result = 31 * result + (int) (id ^ (id >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return "Segment{" +
                "filename='" + filename + '\'' +
                ", id=" + id +
                ", clients=" + clients +
                '}';
    }
}
